package com.hrhx.duhongming.adapter.echarts;

import java.util.Date;
import java.util.Map;

import com.github.abel533.echarts.axis.ValueAxis;
import com.github.abel533.echarts.code.AxisType;
import com.github.abel533.echarts.code.LineType;
import com.github.abel533.echarts.code.PointerType;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Symbol;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Bar;
import com.github.abel533.echarts.style.LineStyle;
import com.hrhx.duhongming.adapter.EchartsAdapter;
import com.hrhx.duhongming.bean.EchartsConfig;

public class EchartsDoubleNumLineAdapter implements EchartsAdapter{
	
	public static final EchartsDoubleNumLineAdapter me = new EchartsDoubleNumLineAdapter();
	
	//double num line的适配器入口
	public GsonOption adapter(EchartsConfig echartsConfig,
			GsonOption gsonOption,
			Map<String,Object[][]> data){
		//EchartsDoubleNumLineAdapter barAdapter = new EchartsDoubleNumLineAdapter();
		gsonOption = me.tooltipAdapter(gsonOption);
		gsonOption = me.legendAdapter(gsonOption, data);
		gsonOption = me.xAxisAdapter(echartsConfig,gsonOption);
		gsonOption = me.yAxisAdapter(echartsConfig,gsonOption);
		gsonOption = me.seriesAdapter(echartsConfig,gsonOption, data);
		return gsonOption;
	}
	
	public GsonOption tooltipAdapter(GsonOption gsonOption){
		gsonOption.calculable(true);
		gsonOption.tooltip().axisPointer().show(true).type(PointerType.cross)
		.lineStyle(new LineStyle().type(LineType.dashed).width(1));
		return gsonOption;
	}
	
	//legend
	public GsonOption legendAdapter(GsonOption gsonOption,Map<String,Object[][]> data){
		if(data != null && data.size() != 0){
			//获取第一个List中的Map中的值
			for (String key : data.keySet()) {
				gsonOption.legend().data(key);
			}
		}
		return gsonOption;
	}
	
	//x轴配置以及单位配置
	public GsonOption xAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption){
		gsonOption.xAxis(new ValueAxis()
				.splitNumber(echartsConfig.getSplitNumber())
				.minInterval(echartsConfig.getMinInterval())
				.type(AxisType.value).name(echartsConfig.getXaxisName()));
		return gsonOption;
	}
	
	//Y轴单位配置
	public GsonOption yAxisAdapter(EchartsConfig echartsConfig,GsonOption gsonOption){
		//gsonOption.yAxis(new CategoryAxis().type(AxisType.value).name(echartsConfig.getYaxisName()));	
		String[] unitNameArray = echartsConfig.getYaxisName().split(",");
		for (String s : unitNameArray) {
			ValueAxis categoryAxis = new ValueAxis();
			categoryAxis.type(AxisType.value);
			gsonOption.yAxis(categoryAxis.name(s));
		}
		return gsonOption;
	}
	
	//Y轴数据处理
	@SuppressWarnings("unchecked")
	public GsonOption seriesAdapter(EchartsConfig echartsConfig,GsonOption gsonOption,Map<String,Object[][]> axisDataArr){
		Boolean isDateType=false;
		for (String mapkey : axisDataArr.keySet()) {
			Bar line = new Bar();
			// 显示直线，而不是密密麻麻的点，一点都不好看
			line.name(mapkey).type(SeriesType.line).symbol(Symbol.none);
			//line.itemStyle().normal().label().show(true).position(Position.top);
			Object[][] dataArr = axisDataArr.get(mapkey);
			for (int num = 0; num < dataArr.length; num++) {
				//判断是否是日期类型
				Object obj = dataArr[num][0];
				if(obj instanceof String || obj instanceof Date){
					isDateType = true;
				}
				
				line.data().add(dataArr[num]);
			}
			gsonOption.series(line);
		}
		if(isDateType){
			//声明X轴数据为时间
			gsonOption.xAxis().get(0).type(AxisType.time);
		}
		return gsonOption;
	}
}
